//
//  UserNickNameViewController.m
//  danpin
//
//  Created by chuangjia on 26/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "UserNickNameViewController.h"
#import "NSString+Extensions.h"
#import "ZToastManager.h"
#import "BaseModel.h"

@interface UserNickNameViewController ()<UITextFieldDelegate>
{
    
}
@property(nonatomic,strong)void(^actionBlock)(id,id);
@property(nonatomic,strong) UITextField     *txtNickname;
@end

@implementation UserNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    self.rightTitle =@"完成";
    self.navTitle =@"修改昵称";
    UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
    self.view.userInteractionEnabled =YES;
    [self.view addGestureRecognizer:singleRecognizer];
    singleRecognizer=nil;
    //
    NSDictionary *dict =[[ZShare sharedInstance] userInfo];
    NSString *nickname =[NSString stringWithFormat:@"%@",[dict objectForKey:@"nick_name"]];
    self.txtNickname.frame =CGRectMake(PHOTO_FRAME_WIDTH*2, hightAuto(90), DEVICE_WIDTH-PHOTO_FRAME_WIDTH*4, PHOTO_FRAME_WIDTH*4);
    self.txtNickname.text=nickname;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setSelectedCallBack:(void(^)(id,id))callback
{
    self.actionBlock=callback;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            if(self.txtNickname.text.length==0)
            {
                [[ZToastManager ShardInstance] showtoast:@"请输入昵称"];
                return;
            }
            [[ZToastManager ShardInstance] showprogress];
            [BaseModel usernickname:self.txtNickname.text success:^(AFHTTPRequestOperation *operation, id resultObject) {
                
                [[ZToastManager ShardInstance] hideprogress];
                [[ZToastManager ShardInstance] showtoast:[resultObject objectForKey:@"msg"]];
                if ([[resultObject objectForKey:@"code"] intValue]==1)
                {
                     [[ZShare sharedInstance] initUserInfo:resultObject];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"updateuserinfo" object:nil];
                    if (self.actionBlock) {
                        self.actionBlock([ZShare sharedInstance].userInfo,@"user");
                    }
                    if (self.navigationController) {
                        if (self.navigationController.viewControllers.count == 1) {
                            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                        } else {
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    } else {
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }
                    
                }
                
                
            } failure:^(NSError *error) {
                [[ZToastManager ShardInstance] hideprogress];
                DLog(@"您取消了选择图片---%@",error);
            }];
        }
            break;
        default:
            break;
    }
}

-(UITextField *)txtNickname
{
    if (!_txtNickname) {
        self.txtNickname = [[UITextField alloc] initWithFrame:CGRectZero];
        self.txtNickname.placeholder = @" 昵称";
        [self.txtNickname setBorderStyle:UITextBorderStyleRoundedRect];
        self.txtNickname.font = FONT(13);
        self.txtNickname.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_email"];
        self.txtNickname.autocorrectionType = UITextAutocorrectionTypeNo;
        self.txtNickname.autocapitalizationType = UITextAutocapitalizationTypeNone;
        self.txtNickname.returnKeyType = UIReturnKeyNext;
        self.txtNickname.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.txtNickname.delegate=self;
        [self.view addSubview:self.txtNickname];
    }
    return _txtNickname;
}
//按下Done按钮的调用方法，我们让键盘消失
-(void)handleSingleTapFrom
{
    [self.txtNickname resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
@end
